﻿using DBHelper;
using Helpers;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Util.SystemModel;

namespace Util.ServiceDAO
{
    public class UserDAO
    {
        public static int InsertUser(UserModel um)
        {
            string cmdText = @"INSERT INTO T_Sys_User
			(UserId
			,Status
			,UserName
			,LoginName
			,Password
			,CreateId
			,CreateTime
			,UpdateId
			,UpdateTime
			,Sex
			,Mobile
			,LoginTime
			,HeadPhoto
			,IsLock
						)
			VALUES(@UserId
			,@Status
			,@UserName
			,@LoginName
			,@Password
			,@CreateId
			,@CreateTime
			,@UpdateId
			,@UpdateTime
			,@Sex
			,@Mobile
			,@LoginTime
			,@HeadPhoto
			,@IsLock
						);SELECT CAST(scope_identity() AS int)";
            SqlParameter[] paras = new SqlParameter[] {
                new SqlParameter("@UserId", um.UserId)
                ,new SqlParameter("@Status", um.Status)
                ,new SqlParameter("@UserName", um.UserName)
                ,new SqlParameter("@LoginName", um.LoginName)
                ,new SqlParameter("@Password", um.Password)
                ,new SqlParameter("@CreateId", um.CreateId)
                ,new SqlParameter("@CreateTime", um.CreateTime)
                ,new SqlParameter("@UpdateId", um.UpdateId)
                ,new SqlParameter("@UpdateTime", um.UpdateTime)
                ,new SqlParameter("@Sex", um.Sex)
                ,new SqlParameter("@Mobile", um.Mobile)
                ,new SqlParameter("@LoginTime", um.LoginTime)
                ,new SqlParameter("@HeadPhoto", um.HeadPhoto)
                ,new SqlParameter("@IsLock", um.IsLock)
                            };
            return Convert.ToInt32(DatabaseFactory.ExecuteScalar(cmdText, CommandType.Text, paras));
        }

        public static int UpdateUser(UserModel um)
        {
            string cmdText = @"UPDATE T_Sys_User SET  UserId=@UserId
			,Status=@Status
			,UserName=@UserName
			,LoginName=@LoginName
			,Password=@Password
			,CreateId=@CreateId
			,CreateTime=@CreateTime
			,UpdateId=@UpdateId
			,UpdateTime=@UpdateTime
			,Sex=@Sex
			,Mobile=@Mobile
			,LoginTime=@LoginTime
			,HeadPhoto=@HeadPhoto
			,IsLock=@IsLock
			Where UserID=@UserID ";
            SqlParameter[] paras = new SqlParameter[] {
                new SqlParameter("@UserId", um.UserId)
                ,new SqlParameter("@Status", um.Status)
                ,new SqlParameter("@UserName", um.UserName)
                ,new SqlParameter("@LoginName", um.LoginName)
                ,new SqlParameter("@Password", um.Password)
                ,new SqlParameter("@CreateId", um.CreateId)
                ,new SqlParameter("@CreateTime", um.CreateTime)
                ,new SqlParameter("@UpdateId", um.UpdateId)
                ,new SqlParameter("@UpdateTime", um.UpdateTime)
                ,new SqlParameter("@Sex", um.Sex)
                ,new SqlParameter("@Mobile", um.Mobile)
                ,new SqlParameter("@LoginTime", um.LoginTime)
                ,new SqlParameter("@HeadPhoto", um.HeadPhoto)
                ,new SqlParameter("@IsLock", um.IsLock)
                };
            return DatabaseFactory.ExecuteNonQuery(cmdText, CommandType.Text, paras);
        }

        public static List<UserModel> SelectUserByWhere(string where, MySqlParameter[] paras, string queue)
        {
            DataTable dt = new DataTable();
            List<UserModel> list = new List<UserModel>();
            string cmdText = "SELECT * FROM T_Sys_User WHERE 1 = 1 {0}{1}";
            cmdText = string.Format(cmdText, where, queue);
            dt = Helpers.MySqlHelper.ExecuteDataTable(cmdText, paras);
            foreach (DataRow dr in dt.Rows)
            {
                UserModel um = new UserModel();
                um.UserId = dr["UserId"].ToString();
                um.Status = dr["Status"].ToString();
                um.UserName = dr["UserName"].ToString();
                um.LoginName = dr["LoginName"].ToString();
                um.Password = dr["Password"].ToString();
                um.CreateId = dr["CreateId"].ToString();
                um.CreateTime = dr["CreateTime"].ToString();
                um.UpdateId = dr["UpdateId"].ToString();
                um.UpdateTime = dr["UpdateTime"].ToString();
                um.Sex = dr["Sex"].ToString();
                um.Mobile = dr["Mobile"].ToString();
                um.LoginTime = dr["LoginTime"].ToString();
                um.HeadPhoto = dr["HeadPhoto"].ToString();
                um.IsLock = dr["IsLock"].ToString();
                list.Add(um);
            }
            return list;
        }

        public static UserModel GetUserModel(string UserID)
        {
            string where = " and UserID=@UserID";
            MySqlParameter[] paras = new MySqlParameter[] {
             new MySqlParameter("@UserID", UserID)
             };
            return SelectUserByWhere(where, paras, "").FirstOrDefault();
        }

        public static bool UpdateUserStatus(string UserID, string status, string userId)
        {
            var list = GetUserModel(UserID);
            if (list != null)
            {
                list.Status = status;
                list.UpdateTime = DateTime.Now.ToString();
                list.UpdateId = userId;
                if (UpdateUser(list).Equals(1))
                    return true;
                return false;
            }
            else
            {
                return false;
            }
        }

    }
}
